package com.ly.web.admin.service.demo.query;

import com.ly.common.base.query.BaseQuery;
import org.hibernate.validator.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * demo的query
 * @Auther: lzw
 * @Date: 2018/6/5
 */
public class DemoQuery<T> extends BaseQuery {

    Logger logger= LoggerFactory.getLogger(this.getClass());

    @NotNull(message = "id不能为空")
    private Integer id;
    private String code;
    private String name;



    @Override
    public Specification toSpecification() {
        Specification querySpecifi = new Specification<T>() {
            @Override
            public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();//条件集合
                List<Order> orderList = new ArrayList<Order>();//排序集合
                String[] ordersArr=getOrders().split(getOrdersSeparator1());

                //----------------条件开始-------------------
                if (!StringUtils.isEmpty(id)) {
                    list.add(criteriaBuilder.equal(root.get("id").as(Integer.class), id));
                }
                if (!StringUtils.isEmpty(code)) {
                    list.add(criteriaBuilder.equal(root.get("code").as(String.class), code));
                }
                if (!StringUtils.isEmpty(name)) {
                    list.add(criteriaBuilder.like(root.get("name").as(String.class), name + "%"));
                }
                //----------------条件结束-------------------

                //----------------排序开始-------------------
                if(!StringUtils.isEmpty(getOrders()) && ordersArr.length>0){
                    try {
                        for (String str:ordersArr){
                            String[] strArr=str.split(getOrdersSeparator2());
                            orderList.add("desc".equalsIgnoreCase(strArr[1])?criteriaBuilder.desc(root.get(strArr[0])):criteriaBuilder.asc(root.get(strArr[0])));
                        }
                    }catch (Exception ex){
                        logger.error("排序字段参数错误!跳过排序");
                    }
                }
                criteriaQuery.orderBy(orderList);
                //----------------排序结束-------------------
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        };
        return querySpecifi;
    }



    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
